<?php
/*
 * Created on Feb 19, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class GeneralInfoModel{	
	
	function GeneralInfoModel(){
	
	}
	
	function getCountInfo($typeId = null){		
		global $connDB;  //The database connection
		if($typeId == null) return null;
		$sql = "SELECT * FROM ".TBL_ITEM_DETAIL." WHERE deleteFlag = 0 AND rees_contenttype_id = " . $typeId;
		$result = $connDB->DB_query($sql, $connDB->connection);
      	return $connDB->DB_num_rows($result);
	}
	
	function getInfos($typeId = null, $offset = null, $limit = null){
		global $connDB;  //The database connection
		if($typeId == null) return null;
		$sql = "SELECT * 
				FROM ".TBL_ITEM_DETAIL." 
				WHERE deleteFlag = 0 AND rees_contenttype_id = ".$typeId." 
				ORDER BY sortNo DESC, updateDate DESC, title ASC ".getLimitFormat($offset, $limit);		
		$result = $connDB->DB_query($sql,$connDB->connection);
      	return $result;
	}
	
	function getArrayInfos($resultSet = null){
		global $connDB;  //The database connection
		if($resultSet == null) return null;
		return $connDB->DB_fetch_row($resultSet);
	}
	
	function getArrayNameInfos($resultSet = null){
		global $connDB;  //The database connection
		if($resultSet == null) return null;
		return $connDB->DB_fetch_array($resultSet);
	}
	
	function getInfoById($type = null, $id = null){
		global $connDB;  //The database connection
		if($type == null || $id == null) return null;
		$sql = "SELECT rit.*, rct.name 
				FROM ".TBL_CONTENT_TYPE." rct 
				INNER JOIN ".TBL_ITEM_DETAIL." rit ON rct.id = rit.rees_contenttype_id 
				WHERE rit.deleteFlag = 0 AND rct.id = ".$type." AND rit.id = ".$id;						
		$result = $connDB->DB_query($sql,$connDB->connection);
		return $connDB->DB_fetch_array($result);
	}
	
	function getInfoTypeById($id = null){
		global $connDB;  //The database connection
		if($id == null) return null;
		$sql = "SELECT rct.name 
				FROM ".TBL_CONTENT_TYPE." rct 
				WHERE rct.id = ".$id;
		$result = $connDB->DB_query($sql,$connDB->connection);
		return $connDB->DB_fetch_array($result);
	}
	
	function addNewInfo($type, $title, $header, $content, $sortno, $istop, $createDate){
		global $connDB;  //The database connection
		
		$createDate = FormatDateForSQL($createDate).' '.date('h:i:s');
		
		$fileData = fread(fopen($image['tmp_name'], "r"), filesize($image['tmp_name']));
		$vowels = array("<p>", "'");		
		$content = str_replace($vowels,'',$content);
		$sql = "INSERT INTO ".TBL_ITEM_DETAIL." (rees_contenttype_id,
						title,										
						header,
						content,
						sortNo,
						isTop,
						deleteFlag,
						createDate,
						updatedate) 
				VALUES (" . $type . ",					
					'" . $title . "',												
					'" . $header . "',
					'" . $content . "',
					". $sortno .",
					". $istop .",
					". 0 .",
					'" . $createDate . "',
					'". $createDate . "')";					
		$msg = _('Thông tin <') . ' ' . $title . ' ' . _('> đã thêm vào hệ thống');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);		
		if($result){			
			return 0;
		}else{
			return 2;
		}
	}
	
	function updateImageField($id = null, $value = null){
		global $connDB;  //The database connection
		if($id == null || $value == null) return null;
		$sql = "UPDATE ".TBL_ITEM_DETAIL." SET image = '$value' WHERE id = $id";
      	return $connDB->DB_query($sql,$connDB->connection);
	}
	
	function editInfo($id, $type, $title, $header, $content, $sortno, $istop){
		global $connDB;  //The database connection
		$vowels = array("<p>", "'");		
		$content = str_replace($vowels,'',$content);		
		$sql = "UPDATE ".TBL_ITEM_DETAIL." SET title='" . $title . "',
						header='" . $header . "',
						content='" . $content . "',
						sortNo=" . $sortno . ",
						isTop=" . $istop . ",
						updateDate='".date('Y-m-d h:i:s')."' 	 				
				WHERE id = ".$id." AND rees_contenttype_id = ".$type;
		$msg = _('Thông tin <') . ' ' . $title . ' ' . _('> đã cập nhật');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function deleteInfo($id){
		global $connDB;  //The database connection
		$sql = "UPDATE ".TBL_ITEM_DETAIL." SET deleteFlag=" . 1 . ",
						updateDate='" . date('Y-m-d h:i:s') . "'
				WHERE id = ".$id;			
		$msg = _('Thông tin <') . ' ' . $id . ' ' . _('> đã xóa');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function getFistNewItem($contentTypeId = null) {
		global $connDB;  //The database connection
		$sql = 	" SELECT `id` itemDetailId, `title`, `rees_contenttype_id` FROM ".TBL_ITEM_DETAIL.
				" WHERE rees_contenttype_id = ".$contentTypeId." AND deleteFlag = 0".
				" ORDER BY updateDate DESC".
				" LIMIT 1";
		
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		return $connDB->DB_fetch_array($result);
	}
	
	function getNewItems($contentTypeId = null) {
		global $connDB;  //The database connection
		$sql = 	" SELECT `id`, `title`, `updateDate` FROM ".TBL_ITEM_DETAIL.
				" WHERE rees_contenttype_id = ".$contentTypeId." AND deleteFlag = 0".
				" ORDER BY updateDate DESC, title ASC".
				" LIMIT ". MAX_RECORD_NEW ."";
		$result = $connDB->DB_query($sql, $connDB->connection);
		return $result;
	}
	
	function getItemDetailById($id = null) {
		global $connDB;  //The database connection
		$sql = 	" SELECT * FROM ".TBL_ITEM_DETAIL.
				" WHERE id = ".$id." AND deleteFlag = 0";
		$itemDetail = $connDB->DB_query($sql, $connDB->connection);
		return $connDB->DB_fetch_array($itemDetail);
	}
	
	function getItemsContentTypeById($contentTypeId = null, $offset = null, $limit = null) {
		global $connDB;  //The database connection
		$sql = 	" SELECT * FROM ".TBL_ITEM_DETAIL.
				" WHERE rees_contenttype_id = ".$contentTypeId." AND deleteFlag = 0".
				" ORDER BY sortNo DESC, createDate DESC, title ASC " . getLimitFormat($offset, $limit);
		$result = $connDB->DB_query($sql, $connDB->connection);
		
		return $result;
	}
	
	function getLastNewItemByContentTypeId($contentTypeId = null, $maxGet = 5) {
		global $connDB;  
		$sql = 	" SELECT * FROM ".TBL_ITEM_DETAIL.
				" WHERE rees_contenttype_id = ".$contentTypeId." AND deleteFlag = 0 AND isTop = 1".
				" ORDER BY createDate DESC LIMIT ".$maxGet; 
		$result = $connDB->DB_query($sql, $connDB->connection);
		
		return $result;
	}
	
	function countItemContentType($contentTypeId = null) {
		global $connDB;  //The database connection
		$sql = 	" SELECT id FROM ".TBL_ITEM_DETAIL.
				" WHERE rees_contenttype_id = ".$contentTypeId." AND deleteFlag = 0";
		 
		$result = $connDB->DB_query($sql, $connDB->connection); 
		return $connDB->DB_num_rows($result);
	}
	
	/*
	 * Get company info
	 * Limit one record
	 * 
	 * @return: Info of company
	 */
	function getCompanyInfo() {
		global $connDB;   
		$sql = 	" SELECT 
						id,
						companyName,
						copyright,
						address,
						phone,
						fax,
						website,
						email,
						createDate,
						updateDate".
				" FROM ".TBL_INFO.
				" ORDER BY updateDate DESC".
				" LIMIT 1";
		$result = $connDB->DB_query($sql, $connDB->connection);
		
		return $connDB->DB_fetch_array($result);
	}
	
	function updateInfoWebsite($id = null, $arrayData = null){
		global $connDB;   
		if(!isset($id)) return 2;
		$sql = "UPDATE ".TBL_INFO." SET ".
					" companyName='" . $arrayData['company'] . "'," .
					" copyright='" . $arrayData['copyright'] . "'," .
					" address='" . $arrayData['address'] . "'," .
					" phone='" . $arrayData['phone'] . "'," .
					" fax='" . $arrayData['fax'] . "'," .
					" website='" . $arrayData['website'] . "'," .
					" email='" . $arrayData['email'] . "'," .	
					"updateDate='" . date('Y-m-d h:i:s') . "' " .
				" WHERE id = '$id'";
		$msg = _('Thông tin website <') . ' ' . $arrayData['company'] . ' ' . _('> đã cập nhật');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function getBanner(){
		global $connDB;  //The database connection
		$sql =	" SELECT * FROM ".TBL_BANNER_IMG.
				" WHERE deleteFlag = 0".
				" ORDER BY sortNo ASC, createDate ASC";
		$result = $connDB->DB_query($sql,$connDB->connection);
      	return $result;
	}
	
	function getBannerById($id = null){
		global $connDB;  //The database connection
		if($id == null) return null;
		$sql = "SELECT * 
				FROM ".TBL_BANNER_IMG." 
				WHERE id = ".$id;
		$result = $connDB->DB_query($sql,$connDB->connection);
		return $connDB->DB_fetch_array($result);
	}
	
	function addNewBanner($arrayData = null){
		global $connDB;  //The database connection
		$createDate = FormatDateForSQL($arrayData['createDate']).' '.date('h:i:s');
		$sql = "INSERT INTO ".TBL_BANNER_IMG." (".
					" sortNo,".
					" url,".
					" createDate) ".
				"VALUES (" . $arrayData['sortNo'] . ",
					'" . $arrayData['url'] . "',
					'" . $createDate . "')";
		$msg = _('Ảnh quảng cáo <') . ' ' . $arrayData['title'] . ' ' . _('> đã thêm vào hệ thống');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function editBanner($id = null, $arrayData = null){
		global $connDB;   
		if(!isset($id)) return 2;
		$sql = "UPDATE ".TBL_BANNER_IMG." SET ".
					" sortNo=" . $arrayData['sortNo'] . "," .
					" url='" . $arrayData['url'] . "'," .
					"createDate='" . date('Y-m-d h:i:s') . "' " .
				" WHERE id = '$id'";
		$msg = _('Ảnh quảng cáo <') . ' ' . $arrayData['company'] . ' ' . _('> đã cập nhật');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function deleteBanner($id){
		global $connDB;  //The database connection
		$sql = "UPDATE ".TBL_BANNER_IMG." SET deleteFlag=" . 1 . ",
						createDate='" . date('Y-m-d h:i:s') . "'
				WHERE id = '$id'";			
		$msg = _('Ảnh quảng cáo <') . ' ' . $id . ' ' . _('> xóa');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function updateBannerImageField($id = null, $value = null){
		global $connDB;  //The database connection
		if($id == null || $value == null) return null;
		$sql = "UPDATE ".TBL_BANNER_IMG." SET image = '$value' WHERE id = $id";
      	return $connDB->DB_query($sql,$connDB->connection);
	}
	
	function getInfoTitleById($id = null) {
		global $connDB;   
		if ($id == null) return null;
		$sql = 	"SELECT  item.title FROM ".TBL_ITEM_DETAIL. " item".
				" WHERE item.id = ".$id;
		$result = $connDB->DB_query($sql, $connDB->connection);		
		return $connDB->DB_fetch_array($result);
	}
};

/* Create info controller object */
$infoModel = new GeneralInfoModel;
?>
